package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.zhgd.util.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.zhgd.domain.bo.ZjExperimentBindingBo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NLaboratoryAsphaltSofteningPointResBo;
import com.ruoyi.zhgd.domain.vo.NLaboratoryAsphaltSofteningPointResVo;
import com.ruoyi.zhgd.domain.NLaboratoryAsphaltSofteningPointRes;
import com.ruoyi.zhgd.mapper.NLaboratoryAsphaltSofteningPointResMapper;
import com.ruoyi.zhgd.service.INLaboratoryAsphaltSofteningPointResService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 沥青软化点试验仪_结果Service业务层处理
 *
 * @author zyb
 * @date 2023-09-11
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NLaboratoryAsphaltSofteningPointResServiceImpl implements INLaboratoryAsphaltSofteningPointResService {

    private final NLaboratoryAsphaltSofteningPointResMapper baseMapper;

    /**
     * 查询沥青软化点试验仪_结果
     */
    @Override
    public NLaboratoryAsphaltSofteningPointResVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询沥青软化点试验仪_结果列表
     */
    @Override
    public TableDataInfo<NLaboratoryAsphaltSofteningPointResVo> queryPageList(NLaboratoryAsphaltSofteningPointResBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NLaboratoryAsphaltSofteningPointRes> lqw = buildQueryWrapper(bo);
        Page<NLaboratoryAsphaltSofteningPointResVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询沥青软化点试验仪_结果列表
     */
    @Override
    public List<NLaboratoryAsphaltSofteningPointResVo> queryList(NLaboratoryAsphaltSofteningPointResBo bo) {
        LambdaQueryWrapper<NLaboratoryAsphaltSofteningPointRes> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NLaboratoryAsphaltSofteningPointRes> buildQueryWrapper(NLaboratoryAsphaltSofteningPointResBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NLaboratoryAsphaltSofteningPointRes> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceId()), NLaboratoryAsphaltSofteningPointRes::getDeviceId, bo.getDeviceId());
        lqw.eq(StringUtils.isNotBlank(bo.getFlag()), NLaboratoryAsphaltSofteningPointRes::getFlag, bo.getFlag());
        lqw.eq(bo.getMin() != null, NLaboratoryAsphaltSofteningPointRes::getMin, bo.getMin());
        lqw.eq(bo.getSecond() != null, NLaboratoryAsphaltSofteningPointRes::getSecond, bo.getSecond());
        lqw.eq(bo.getNum1() != null, NLaboratoryAsphaltSofteningPointRes::getNum1, bo.getNum1());
        lqw.eq(bo.getTemp1() != null, NLaboratoryAsphaltSofteningPointRes::getTemp1, bo.getTemp1());
        lqw.eq(bo.getNum2() != null, NLaboratoryAsphaltSofteningPointRes::getNum2, bo.getNum2());
        lqw.eq(bo.getTemp2() != null, NLaboratoryAsphaltSofteningPointRes::getTemp2, bo.getTemp2());
        lqw.eq(StringUtils.isNotBlank(bo.getOriginalData()), NLaboratoryAsphaltSofteningPointRes::getOriginalData, bo.getOriginalData());
        lqw.eq(StringUtils.isNotBlank(bo.getSampleId()), NLaboratoryAsphaltSofteningPointRes::getSampleId, bo.getSampleId());
        return lqw;
    }

    /**
     * 新增沥青软化点试验仪_结果
     */
    @Override
    public Boolean insertByBo(NLaboratoryAsphaltSofteningPointResBo bo) {
        NLaboratoryAsphaltSofteningPointRes add = BeanUtil.toBean(bo, NLaboratoryAsphaltSofteningPointRes.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改沥青软化点试验仪_结果
     */
    @Override
    public Boolean updateByBo(NLaboratoryAsphaltSofteningPointResBo bo) {
        NLaboratoryAsphaltSofteningPointRes update = BeanUtil.toBean(bo, NLaboratoryAsphaltSofteningPointRes.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NLaboratoryAsphaltSofteningPointRes entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除沥青软化点试验仪_结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public PageUtils queryPage(Page page, Map<String, Object> params) {
        List<Map> list = baseMapper.queryPages(page, params);
        return new PageUtils(page.setRecords(list));
    }

    @Override
    public List<NLaboratoryAsphaltSofteningPointRes> selectSofteningPoint(List<String> ids) {
        List<NLaboratoryAsphaltSofteningPointRes> softeningPointRes = baseMapper.selectBatchIds(ids);
        return softeningPointRes;
    }

    @Override
    public void updateByBos(List<NLaboratoryAsphaltSofteningPointRes> softeningPointRes) {
        baseMapper.updateBatchById(softeningPointRes);
    }

    @Override
    public Page<Map<String, Object>> selectRhdPage(ZjExperimentBindingBo bo, Page<Object> build) {
        return baseMapper.selectRhdPage(bo,build);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateStatus(String laboratoryId, Integer eligible, Integer relevance) {
        baseMapper.updateStatus(laboratoryId, eligible, relevance);
    }

    @Override
    public String selectDeptId(String deviceId) {
        return baseMapper.selectDeptId(deviceId);
    }
}
